<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
// import { debounce } from "@/utils";
export default {
  props: {
    //   从组件调用者传进来的样式类
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    // 折线图的数据项
    chartData: {
      type: Array,
      required: true,
    },
    // 折线图的标题
    chartTitle: {
      type: String,
      default: "",
    },
  },

  //   组件data
  data() {
    return {
      chart: null,
    };
  },
  // 组件属性监视
  watch: {
    // 监视chart的数据
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },

  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  //   销毁chart组件
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },

  // 组件方法
  methods: {
    // 初始化表格chart
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    // 配置折线图option
    setOptions(data) {
      this.chart.setOption({
        title: {
          text: this.chartTitle,
          textStyle: {
            color: "#000",
            fontStyle: "normal",
            fontSize: 18,
          },
          left: "center",
        },
        xAxis: {
          type: "category",
          //   data: ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"],
          //   刻度只是作为分隔线，标签和数据点都会在两个刻度之间的带(band)中间
          boundaryGap: false,
          //   坐标轴刻度相关设置
          axisTick: {
            //   显示坐标轴刻度
            show: false,
          },
        },
        yAxis: {
          axisLine: {
            show: true,
          },
        },
        // 直角坐标系内绘图网格
        grid: {
          // grid 组件离容器左侧的距离
          left: 10,
          right: 10,
          bottom: 20,
          top: 30,
          //   grid 区域是否包含坐标轴的刻度标签
          containLabel: true,
        },
        // 图例
        legend: {},
        tooltip: {
          trigger: "axis",
          // 坐标轴指示器配置项
          axisPointer: {
            type: "cross",
          },
          // 提示框浮层内边距 上下的内边距为 5，左右的内边距为 10
          padding: [5, 10],
        },
        // 工具栏
        toolbox: {
          // 各工具配置项
          feature: {
            //   数据视图
            dataView: { show: true, readOnly: false },
            // 动态类型切换
            magicType: { show: true, type: ["line", "bar"] },
            // chart表格重置项
            restore: { show: true },
            // 保存为图片
            saveAsImage: { show: true },
          },
        },
        // 系列列表
        series: [
          {
            //   平滑样式
            smooth: true,
            type: "line",
            itemStyle: {
              normal: {
                color: "#3888fa",
                lineStyle: {
                  color: "#3888fa",
                  width: 2,
                },
                areaStyle: {
                  color: "#f3f8ff",
                },
              },
            },
            data: data,
            animationDuration: 2800,
            animationEasing: "quadraticOut",
          },
        ],
      });
    },
  },
};
</script>

<style>
</style>